package io.yizoo.business.controller;

import com.fasterxml.jackson.databind.node.ObjectNode;
import io.elm.core.CoreController;
import io.yizoo.business.entity.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

@RestController
@RequestMapping("/business/jxc/goods/dict")
public class jxc_goods_dictController extends CoreController {

    @Override
    protected void Init() {
        PowerEntity = biz_jxc_goods_dict.class;
        PowerUse = EnumSet.of(Power.IDDelete, Power.IDAny, Power.IDGetSingle,Power.IDUpsert);
    }

    @PostMapping("/GetData")
    public Object GetData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("t.*,ifnull(a.name,t.type) as type_mc")
                        .From(biz_jxc_goods_dict.class,"t")
                        .LeftJoin(biz_jxc_goods_type.class,"a","a.id=t.type")
                        .IsWhere("and t.name like concat('%', #{name}, '%')", "name")
                        .IsWhere("and t.id = #{id}", "id")
                        .OrderBy("t.name")
                        .IsPage()
                        .GetFactor());
        return JsonResultPage(ret);
    }

    @PostMapping("/GetGoodsData")
    public Object GetGoodsData(@RequestBody String jsonStr) {
        ObjectNode objectNode = Ctor.getObjectNode(jsonStr);
        if(!objectNode.has("id") || objectNode.get("id").asText() == null ||objectNode.get("id").asText().equals("")) {
            return JsonResult(new ArrayList<>());
        }
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("t.type,a.name as type_mc,t.unit")
                        .From(biz_jxc_goods_dict.class,"t")
                        .LeftJoin(biz_jxc_goods_type.class,"a","a.id=t.type")
                        .IsWhere("and t.id=#{id}", "id")
                        .GetFactor());
        return JsonResult(ret);
    }

}